﻿using System;
using System.Web;

/// <summary>
/// Summary description for _5presnter
/// </summary>
/// 

namespace Bestlife.ERP.BusinessRules
{
    public class _5presnter : System.Web.UI.Page
    {
        Double direect_commision;
        String Parent_Ststus;
        Double indireect_commision;
        Double Parentinderect;
        Double Totalbv;
        Double TotalCommitions;
        Double bv;
        String Current_user;
        Double Olduserst;
        Boolean olderuserst;

        public _5presnter()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        

        public void presentercal_5(String id, Double product_bv, Double product_pv, Double ts, String user_status,string olderuser)
        {

            Bestlife.ERP.DataAccess.D_bonus bns = new DataAccess.D_bonus();//(Add by chathuranga)
            Bestlife.ERP.Entities.E_Bonus ebns1 = new Entities.E_Bonus();//(Add by chathuranga)
            Bestlife.ERP.Entities.E_Bonus ebns2 = new Entities.E_Bonus();//(Add by chathuranga)
            Totalbv = product_bv;
            bv = product_bv;

            try
            {

                    //total sales  direct commition for 5 presenter
                    direect_commision = ((product_bv * 5.0) / 100);

                    ebns1 = bns.Getbonus(id);//get user(child) totalcommition(Edit by chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )
                    double T_commitioninDB = double.Parse(ebns1.T_commision.ToString());

                    DataAccess.parents p1 = new DataAccess.parents();

                    string parent= p1.parent(id);// add parent id (Edit by chathuranga 22-06-2012)
                    ebns2 = bns.Getbonus(parent);// add parent id (Edit by chathuranga 22-06-2012)
                    double parent_tot_commition = double.Parse(ebns2.T_commision.ToString());
                   
                    Parent_Ststus = (Parent_Ststus = p1.ParentDetails(id));// get parent status


                    if (Parent_Ststus != null)
                    {
                        double parent_St = double.Parse(Parent_Ststus);
                        double child_St = double.Parse(user_status);

                        #region Older_status

                        if (!olderuser.Equals("0"))
                        {
                            DataAccess.D_bonus b2 = new DataAccess.D_bonus();
                            string Olduser = b2.Getbonus(olderuser).User_Status;
                            Olduserst = double.Parse(Olduser);
                            if (parent_St > Olduserst)
                            {
                                olderuserst = true;
                            }
                            else
                            {
                                olderuserst = false;
                            }
                        }
                        else { olderuserst = true; }

                        #endregion //ADD BY CHATHURANGA WEERAKOON -26-11-2012-12-21PM

                         indireect_commision = ((Totalbv * 4.0) / 100);// inderirect commision 
                         Parentinderect = indireect_commision + parent_tot_commition;
                         TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                    if (child_St > parent_St && olderuserst == true) 
                    {
                        Current_user = Session["user"].ToString();
                        Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();
                        DataAccess.users crnt = new DataAccess.users();
                        String CrntID = crnt.GetUserId(Current_user);
                        if (Session["olderuser"].ToString().Equals("0"))
                        {
                            Session["olderuser"] = id;
                        }
                        else
                        {
                            Session["olderuser"] = olderuser;
                        }

                        olderuser = Session["olderuser"].ToString();

                        p1.UpdateParenttpvmonth(parent, product_bv, product_pv, user_status);//update parent tppv month and tbvmonth add by chathuranga 04-08-2012

                        if ((id == CrntID))
                        {

                            Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                        }

                        DataAccess.users usr = new DataAccess.users();
                        usr.users_level(parent, product_bv, product_pv, ts, olderuser);


                    }
                    else if (olderuserst == false) //add by chathuranga -15-10-2012
                    {

                        Parentinderect = indireect_commision + parent_tot_commition;
                        TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                        Current_user = Session["user"].ToString();
                        Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();
                        DataAccess.users crnt = new DataAccess.users();
                        String CrntID = crnt.GetUserId(Current_user);

                        p1.UpdateParenttpvmonth(parent, product_bv, product_pv, user_status);//update parent tppv month and tbvmonth add by chathuranga 04-08-2012

                        if ((id == CrntID))
                        {

                            Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                        }


                        DataAccess.users usr = new DataAccess.users();
                        usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                    }
                    else if (user_status.Equals("2") && Parent_Ststus.Equals("2") && olderuserst == true)    //  Ruby and Ruby
                    {
                        indireect_commision = ((Totalbv * 4.0) / 100);// inderirect commision 
                        Parentinderect = indireect_commision + parent_tot_commition;
                        TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                        Current_user = Session["user"].ToString();
                        Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();
                        DataAccess.users crnt = new DataAccess.users();
                        String CrntID = crnt.GetUserId(Current_user);
                         //if (Session["olderuser"].ToString().Equals("0"))
                        //{
                        //    Session["olderuser"] = id;
                        //}
                        //else
                        //{
                        //    Session["olderuser"] = olderuser;
                        //}

                        //olderuser = Session["olderuser"].ToString();
                        p1.UpdateParenttpvmonth(parent, product_bv, product_pv, user_status);//update parent tppv month and tbvmonth add by chathuranga 04-08-2012

                        if ((id == CrntID))
                        {

                            Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                        }


                        DataAccess.users usr = new DataAccess.users();
                        usr.users_level(parent, product_bv, product_pv, ts,olderuser);


                    }

                    else if (user_status.Equals("2") && Parent_Ststus.Equals("3") && olderuserst == true)  //  5% and 10% presenter
                     {
                            
                        //============================================================================================================================

                         Boolean check = false;
                         if (olderuser != "0")
                         {
                             DataAccess.D_bonus b2 = new DataAccess.D_bonus();
                             string Olduser = b2.Getbonus(olderuser).User_Status;
                             Olduserst = double.Parse(Olduser);

                             if (Olduserst == 5.1 && Parent_Ststus.Equals("3"))
                             {

                                 Parentinderect = indireect_commision + parent_tot_commition;
                                 TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                 Current_user = Session["user"].ToString();
                                 Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();
                                 DataAccess.users crnt = new DataAccess.users();
                                 String CrntID = crnt.GetUserId(Current_user);

                                 //if (Session["olderuser"].ToString().Equals("0"))
                                 //{
                                 //    Session["olderuser"] = id;
                                 //}
                                 //else
                                 //{
                                 //    Session["olderuser"] = olderuser;
                                 //}

                                 //olderuser = Session["olderuser"].ToString();

                                 p1.UpdateParenttpvmonth(parent, product_bv, product_pv, user_status);//update parent tppv month and tbvmonth 

                                 if ((id == CrntID))
                                 {

                                     Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                 }



                                 // Update user commisions + update parent cpv+ user pv
                                 // Sales.Totalsales(user_id,TotalCommisions);  
                                 Session["olderuser"] = "0";
                                 olderuser = Session["olderuser"].ToString();
                                 DataAccess.users usr = new DataAccess.users();
                                 usr.users_level(parent, product_bv, product_pv, ts, olderuser);
                                 check = true;
                             }
                             else if (user_status.Equals("2") && Parent_Ststus.Equals("3") && check == false)
                             {
                                 Session["olderuser"] = "0";


                                 indireect_commision = ((Totalbv * 5.0) / 100);// inderirect commision 

                                 Parentinderect = indireect_commision + parent_tot_commition;
                                 TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                 // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                                 // update parent bv and cpv
                                 DataAccess.parents pr = new DataAccess.parents();
                                 Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                                 Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )

                                 // Get Current user Id
                                 Current_user = Session["user"].ToString();

                                 DataAccess.users crnt = new DataAccess.users();
                                 String CrntID = crnt.GetUserId(Current_user);

                                 if ((id == CrntID))
                                 {

                                     Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                 }

                                 // Update user commisions + update parent cpv+ user pv
                                 // Sales.Totalsales(user_id,TotalCommisions);  

                                 DataAccess.users usr = new DataAccess.users();
                                 usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                             }

                         }
                         else if (user_status.Equals("2") && Parent_Ststus.Equals("3") && check == false)
                         {
                             Session["olderuser"] = "0";


                             indireect_commision = ((Totalbv * 5.0) / 100);// inderirect commision 

                             Parentinderect = indireect_commision + parent_tot_commition;
                             TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                             // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                             // update parent bv and cpv
                             DataAccess.parents pr = new DataAccess.parents();
                             Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                             Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )

                             // Get Current user Id
                             Current_user = Session["user"].ToString();

                             DataAccess.users crnt = new DataAccess.users();
                             String CrntID = crnt.GetUserId(Current_user);

                             if ((id == CrntID))
                             {

                                 Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                             }

                             // Update user commisions + update parent cpv+ user pv
                             // Sales.Totalsales(user_id,TotalCommisions);  

                             DataAccess.users usr = new DataAccess.users();
                             usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                         }
                        
                        
                   
                       //==============================================================================================================================
                        //============================================================================================================================
                        ////total sales  direct commition for 5 presenter
                        //    indireect_commision = ((Totalbv * 5.0) / 100);// inderirect commision 

                        //    Parentinderect = indireect_commision + parent_tot_commition;
                        //    TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                        //    // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                        //    // update parent bv and cpv
                        //    DataAccess.parents pr = new DataAccess.parents();
                        //    Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                        //    Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                        //    // Get Current user Id
                        //    Current_user = Session["user"].ToString();

                        //    DataAccess.users crnt = new DataAccess.users();
                        //    String CrntID = crnt.GetUserId(Current_user);

                        //    if ((id == CrntID))
                        //    {

                        //        Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                        //    }

                        //    // Update user commisions + update parent cpv+ user pv
                        //    // Sales.Totalsales(user_id,TotalCommisions);  

                        //    DataAccess.users usr = new DataAccess.users();
                        //    usr.users_level(parent, product_bv, product_pv, ts,olderuser);


                        }

                        else if (user_status.Equals("2") && Parent_Ststus.Equals("4") && olderuserst == true)    //  5% and 15% presenter
                        {


                            //============================================================================================================================

                            Boolean check = false;
                            if (olderuser != "0")
                            {
                                DataAccess.D_bonus b2 = new DataAccess.D_bonus();
                                string Olduser = b2.Getbonus(olderuser).User_Status;
                                Olduserst = double.Parse(Olduser);

                                if (Olduserst == 5.1 && Parent_Ststus.Equals("4"))
                                {

                                    Parentinderect = indireect_commision + parent_tot_commition;
                                    TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                    Current_user = Session["user"].ToString();
                                    Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();
                                    DataAccess.users crnt = new DataAccess.users();
                                    String CrntID = crnt.GetUserId(Current_user);

                                    //if (Session["olderuser"].ToString().Equals("0"))
                                    //{
                                    //    Session["olderuser"] = id;
                                    //}
                                    //else
                                    //{
                                    //    Session["olderuser"] = olderuser;
                                    //}

                                    //olderuser = Session["olderuser"].ToString();

                                    p1.UpdateParenttpvmonth(parent, product_bv, product_pv, user_status);//update parent tppv month and tbvmonth 

                                    if ((id == CrntID))
                                    {

                                        Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                    }



                                    // Update user commisions + update parent cpv+ user pv
                                    // Sales.Totalsales(user_id,TotalCommisions);  
                                    Session["olderuser"] = "0";
                                    olderuser = Session["olderuser"].ToString();
                                    DataAccess.users usr = new DataAccess.users();
                                    usr.users_level(parent, product_bv, product_pv, ts, olderuser);
                                    check = true;
                                }
                                else if (user_status.Equals("2") && Parent_Ststus.Equals("4") && check == false)
                                {
                                    Session["olderuser"] = "0";


                                    indireect_commision = ((Totalbv * 10.0) / 100);// inderirect commision 

                                    Parentinderect = indireect_commision + parent_tot_commition;
                                    TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                    // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                                    // update parent bv and cpv
                                    DataAccess.parents pr = new DataAccess.parents();
                                    Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                                    Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )

                                    // Get Current user Id
                                    Current_user = Session["user"].ToString();

                                    DataAccess.users crnt = new DataAccess.users();
                                    String CrntID = crnt.GetUserId(Current_user);

                                    if ((id == CrntID))
                                    {

                                        Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                    }

                                    // Update user commisions + update parent cpv+ user pv
                                    // Sales.Totalsales(user_id,TotalCommisions);  

                                    DataAccess.users usr = new DataAccess.users();
                                    usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                                }

                            }
                            else if (user_status.Equals("2") && Parent_Ststus.Equals("4") && check == false)
                            {
                                Session["olderuser"] = "0";


                                indireect_commision = ((Totalbv * 10.0) / 100);// inderirect commision 

                                Parentinderect = indireect_commision + parent_tot_commition;
                                TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                                // update parent bv and cpv
                                DataAccess.parents pr = new DataAccess.parents();
                                Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                                Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )

                                // Get Current user Id
                                Current_user = Session["user"].ToString();

                                DataAccess.users crnt = new DataAccess.users();
                                String CrntID = crnt.GetUserId(Current_user);

                                if ((id == CrntID))
                                {

                                    Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                }

                                // Update user commisions + update parent cpv+ user pv
                                // Sales.Totalsales(user_id,TotalCommisions);  

                                DataAccess.users usr = new DataAccess.users();
                                usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                            }



                            //==============================================================================================================================
                            //============================================================================================================================






                            ////total sales  direct commition for 5 presenter
                            //indireect_commision = ((Totalbv * 10.0) / 100);// inderirect commision 

                            //Parentinderect = indireect_commision + parent_tot_commition;
                            //TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                            //// TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                            //// update parent bv and cpv
                            //DataAccess.parents pr = new DataAccess.parents();
                            //Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            //Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            //// Get Current user Id
                            //Current_user = Session["user"].ToString();

                            //DataAccess.users crnt = new DataAccess.users();
                            //String CrntID = crnt.GetUserId(Current_user);

                            //if ((id == CrntID))
                            //{

                            //    Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                            //}

                            //// Update user commisions + update parent cpv+ user pv
                            //// Sales.Totalsales(user_id,TotalCommisions);  

                            //DataAccess.users usr = new DataAccess.users();
                            //usr.users_level(parent, product_bv, product_pv, ts,olderuser);

                        }

                    else if (user_status.Equals("2") && Parent_Ststus.Equals("5") && olderuserst == true)   //  5% and Agent
                        {


                            //============================================================================================================================

                            Boolean check = false;
                            if (olderuser != "0")
                            {
                                DataAccess.D_bonus b2 = new DataAccess.D_bonus();
                                string Olduser = b2.Getbonus(olderuser).User_Status;
                                Olduserst = double.Parse(Olduser);

                                if (Olduserst == 5.1 && Parent_Ststus.Equals("5"))
                                {

                                    Parentinderect = indireect_commision + parent_tot_commition;
                                    TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                    Current_user = Session["user"].ToString();
                                    Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();
                                    DataAccess.users crnt = new DataAccess.users();
                                    String CrntID = crnt.GetUserId(Current_user);

                                    //if (Session["olderuser"].ToString().Equals("0"))
                                    //{
                                    //    Session["olderuser"] = id;
                                    //}
                                    //else
                                    //{
                                    //    Session["olderuser"] = olderuser;
                                    //}

                                    //olderuser = Session["olderuser"].ToString();

                                    p1.UpdateParenttpvmonth(parent, product_bv, product_pv, user_status);//update parent tppv month and tbvmonth 

                                    if ((id == CrntID))
                                    {

                                        Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                    }



                                    // Update user commisions + update parent cpv+ user pv
                                    // Sales.Totalsales(user_id,TotalCommisions);  
                                    Session["olderuser"] = "0";
                                    olderuser = Session["olderuser"].ToString();
                                    DataAccess.users usr = new DataAccess.users();
                                    usr.users_level(parent, product_bv, product_pv, ts, olderuser);
                                    check = true;
                                }
                                else if (user_status.Equals("2") && Parent_Ststus.Equals("5") && check == false)
                                {
                                    Session["olderuser"] = "0";


                                    indireect_commision = ((Totalbv * 15.0) / 100);// inderirect commision 

                                    Parentinderect = indireect_commision + parent_tot_commition;
                                    TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                    // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                                    // update parent bv and cpv
                                    DataAccess.parents pr = new DataAccess.parents();
                                    Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                                    Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )

                                    // Get Current user Id
                                    Current_user = Session["user"].ToString();

                                    DataAccess.users crnt = new DataAccess.users();
                                    String CrntID = crnt.GetUserId(Current_user);

                                    if ((id == CrntID))
                                    {

                                        Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                    }

                                    // Update user commisions + update parent cpv+ user pv
                                    // Sales.Totalsales(user_id,TotalCommisions);  

                                    DataAccess.users usr = new DataAccess.users();
                                    usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                                }

                            }
                            else if (user_status.Equals("2") && Parent_Ststus.Equals("5") && check == false)
                            {
                                Session["olderuser"] = "0";


                                indireect_commision = ((Totalbv * 15.0) / 100);// inderirect commision 

                                Parentinderect = indireect_commision + parent_tot_commition;
                                TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                                // update parent bv and cpv
                                DataAccess.parents pr = new DataAccess.parents();
                                Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                                Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )

                                // Get Current user Id
                                Current_user = Session["user"].ToString();

                                DataAccess.users crnt = new DataAccess.users();
                                String CrntID = crnt.GetUserId(Current_user);

                                if ((id == CrntID))
                                {

                                    Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                }

                                // Update user commisions + update parent cpv+ user pv
                                // Sales.Totalsales(user_id,TotalCommisions);  

                                DataAccess.users usr = new DataAccess.users();
                                usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                            }



                            //==============================================================================================================================
                            //============================================================================================================================





                            ////total sales  direct commition for 5 presenter
                            //indireect_commision = ((Totalbv * 15.0) / 100);// inderirect commision 

                            //Parentinderect = indireect_commision + parent_tot_commition;
                            //TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                            //// TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                            //// update parent bv and cpv
                            //DataAccess.parents pr = new DataAccess.parents();
                            //Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            //Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            //// Get Current user Id
                            //Current_user = Session["user"].ToString();

                            //DataAccess.users crnt = new DataAccess.users();
                            //String CrntID = crnt.GetUserId(Current_user);

                            //if ((id == CrntID))
                            //{

                            //    Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 
                            //}

                            //// Update user commisions + update parent cpv+ user pv
                            //// Sales.Totalsales(user_id,TotalCommisions);  

                            //DataAccess.users usr = new DataAccess.users();
                            //usr.users_level(parent, product_bv, product_pv, ts,olderuser);



                        }

                    else if (user_status.Equals("2") && Parent_Ststus.Equals("5.1") && olderuserst == true)   //  5% and Agent_Inf
                    {






                        //============================================================================================================================

                        Boolean check = false;
                        if (olderuser != "0")
                        {
                            DataAccess.D_bonus b2 = new DataAccess.D_bonus();
                            string Olduser = b2.Getbonus(olderuser).User_Status;
                            Olduserst = double.Parse(Olduser);

                            if (Olduserst == 5.1 && Parent_Ststus.Equals("5.1"))
                            {

                                Parentinderect = indireect_commision + parent_tot_commition;
                                TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                Current_user = Session["user"].ToString();
                                Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();
                                DataAccess.users crnt = new DataAccess.users();
                                String CrntID = crnt.GetUserId(Current_user);

                                //if (Session["olderuser"].ToString().Equals("0"))
                                //{
                                //    Session["olderuser"] = id;
                                //}
                                //else
                                //{
                                //    Session["olderuser"] = olderuser;
                                //}

                                //olderuser = Session["olderuser"].ToString();

                                p1.UpdateParenttpvmonth(parent, product_bv, product_pv, user_status);//update parent tppv month and tbvmonth 

                                if ((id == CrntID))
                                {

                                    Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                }



                                // Update user commisions + update parent cpv+ user pv
                                // Sales.Totalsales(user_id,TotalCommisions);  
                                Session["olderuser"] = "0";
                                olderuser = Session["olderuser"].ToString();
                                DataAccess.users usr = new DataAccess.users();
                                usr.users_level(parent, product_bv, product_pv, ts, olderuser);
                                check = true;
                            }
                            else if (user_status.Equals("2") && Parent_Ststus.Equals("5.1") && check == false)
                            {
                                Session["olderuser"] = "0";


                                indireect_commision = ((Totalbv * 15.0) / 100);// inderirect commision 

                                Parentinderect = indireect_commision + parent_tot_commition;
                                TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                                // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                                // update parent bv and cpv
                                DataAccess.parents pr = new DataAccess.parents();
                                Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                                Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )

                                // Get Current user Id
                                Current_user = Session["user"].ToString();

                                DataAccess.users crnt = new DataAccess.users();
                                String CrntID = crnt.GetUserId(Current_user);

                                if ((id == CrntID))
                                {

                                    Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                                }

                                // Update user commisions + update parent cpv+ user pv
                                // Sales.Totalsales(user_id,TotalCommisions);  

                                DataAccess.users usr = new DataAccess.users();
                                usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                            }

                        }
                        else if (user_status.Equals("2") && Parent_Ststus.Equals("5.1") && check == false)
                        {
                            Session["olderuser"] = "0";


                            indireect_commision = ((Totalbv * 15.0) / 100);// inderirect commision 

                            Parentinderect = indireect_commision + parent_tot_commition;
                            TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                            // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                            // update parent bv and cpv
                            DataAccess.parents pr = new DataAccess.parents();
                            Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )

                            // Get Current user Id
                            Current_user = Session["user"].ToString();

                            DataAccess.users crnt = new DataAccess.users();
                            String CrntID = crnt.GetUserId(Current_user);

                            if ((id == CrntID))
                            {

                                Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                            }

                            // Update user commisions + update parent cpv+ user pv
                            // Sales.Totalsales(user_id,TotalCommisions);  

                            DataAccess.users usr = new DataAccess.users();
                            usr.users_level(parent, product_bv, product_pv, ts, olderuser);

                        }



                        //==============================================================================================================================
                        //============================================================================================================================









                        ////total sales  direct commition for 5 presenter
                        //indireect_commision = ((Totalbv * 15.0) / 100);// inderirect commision 

                        //Parentinderect = indireect_commision + parent_tot_commition;
                        //TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                        //// TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                        //// update parent bv and cpv
                        //DataAccess.parents pr = new DataAccess.parents();
                        //Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                        //Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                        //// Get Current user Id
                        //Current_user = Session["user"].ToString();

                        //DataAccess.users crnt = new DataAccess.users();
                        //String CrntID = crnt.GetUserId(Current_user);

                        //if ((id == CrntID))
                        //{

                        //    Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 
                        //}

                        //// Update user commisions + update parent cpv+ user pv
                        //// Sales.Totalsales(user_id,TotalCommisions);  

                        //DataAccess.users usr = new DataAccess.users();
                        //usr.users_level(parent, product_bv, product_pv, ts,olderuser);



                    }

                    else if (user_status.Equals("2") && Parent_Ststus.Equals("6") && olderuserst == true)   //  5% and Ruby
                        {


                            //total sales  direct commition for 5 presenter
                            indireect_commision = ((Totalbv * 19.0) / 100);// inderirect commision 

                            Parentinderect = indireect_commision + parent_tot_commition;
                            TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                            // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                            // update parent bv and cpv
                            DataAccess.parents pr = new DataAccess.parents();
                            Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            // Get Current user Id
                            Current_user = Session["user"].ToString();

                            DataAccess.users crnt = new DataAccess.users();
                            String CrntID = crnt.GetUserId(Current_user);

                            if ((id == CrntID))
                            {

                                Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                            }

                            // Update user commisions + update parent cpv+ user pv
                            // Sales.Totalsales(user_id,TotalCommisions);  

                            DataAccess.users usr = new DataAccess.users();
                            usr.users_level(parent, product_bv, product_pv, ts,olderuser);


                        }

                    else if (user_status.Equals("2") && Parent_Ststus.Equals("7") && olderuserst == true)   //  5% and Diamond
                        {


                            //total sales  direct commition for 5 presenter
                            indireect_commision = ((Totalbv * 23.0) / 100);// inderirect commision 

                            Parentinderect = indireect_commision + parent_tot_commition;
                            TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                            // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                            // update parent bv and cpv
                            DataAccess.parents pr = new DataAccess.parents();
                            Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            // Get Current user Id
                            Current_user = Session["user"].ToString();

                            DataAccess.users crnt = new DataAccess.users();
                            String CrntID = crnt.GetUserId(Current_user);

                            if ((id == CrntID))
                            {

                                Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                            }

                            // Update user commisions + update parent cpv+ user pv
                            // Sales.Totalsales(user_id,TotalCommisions);  

                            DataAccess.users usr = new DataAccess.users();
                            usr.users_level(parent, product_bv, product_pv, ts,olderuser);

                        }

                    else if (user_status.Equals("2") && Parent_Ststus.Equals("8") && olderuserst == true)    //  5% and senior diamond
                        {


                            //total sales  direct commition for 5 presenter
                            indireect_commision = ((Totalbv * 27.0) / 100);// inderirect commision 

                            Parentinderect = indireect_commision + parent_tot_commition;
                            TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                            // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                            // update parent bv and cpv
                            DataAccess.parents pr = new DataAccess.parents();
                            Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            // Get Current user Id
                            Current_user = Session["user"].ToString();

                            DataAccess.users crnt = new DataAccess.users();
                            String CrntID = crnt.GetUserId(Current_user);

                            if ((id == CrntID))
                            {

                                Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                            }

                            // Update user commisions + update parent cpv+ user pv
                            // Sales.Totalsales(user_id,TotalCommisions);  

                            DataAccess.users usr = new DataAccess.users();
                            usr.users_level(parent, product_bv, product_pv, ts,olderuser);


                        }


                            else if (user_status.Equals("2") && Parent_Ststus.Equals("9") && olderuserst == true)    //  5% and senior EXEC diamond
                           {


                            //total sales  direct commition for 5 presenter
                            indireect_commision = ((Totalbv * 31.0) / 100);// inderirect commision 
                            Parentinderect = indireect_commision + parent_tot_commition;
                            TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                            // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                            // update parent bv and cpv
                            DataAccess.parents pr = new DataAccess.parents();
                            Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            // Get Current user Id
                            Current_user = Session["user"].ToString();

                            DataAccess.users crnt = new DataAccess.users();
                            String CrntID = crnt.GetUserId(Current_user);

                            if ((id == CrntID))
                            {

                                Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                            }

                            // Update user commisions + update parent cpv+ user pv
                            // Sales.Totalsales(user_id,TotalCommisions);  

                            DataAccess.users usr = new DataAccess.users();
                            usr.users_level(parent, product_bv, product_pv, ts,olderuser);


                        }


                         else if (user_status.Equals("2") && Parent_Ststus.Equals("10") && olderuserst == true)    //  5% and senior EXEC diamond
                        {


                            //total sales  direct commition for 5 presenter
                            indireect_commision = ((Totalbv * 35.0) / 100);// inderirect commision 

                            Parentinderect = indireect_commision + parent_tot_commition;
                            TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user

                            // TotalCommitions = direect_commision + indireect_commision;//(Edit By chathuranga - Saturday, June 16, 2012 at 2:08:04 PM )

                            // update parent bv and cpv
                            DataAccess.parents pr = new DataAccess.parents();
                            Bestlife.ERP.DataAccess.D_child ch = new DataAccess.D_child();//add child class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            Boolean updated = pr.UpdateParent(id, bv, Parentinderect, product_pv, user_status, indireect_commision);//update parent TotalCommition --> inderect_commition class (by chathuranga - Saturday, June 16, 2012 at 2:32:31 PM )
                            // Get Current user Id
                            Current_user = Session["user"].ToString();

                            DataAccess.users crnt = new DataAccess.users();
                            String CrntID = crnt.GetUserId(Current_user);

                            if ((id == CrntID))
                            {

                                Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 

                            }

                            // Update user commisions + update parent cpv+ user pv
                            // Sales.Totalsales(user_id,TotalCommisions);  

                            DataAccess.users usr = new DataAccess.users();
                            usr.users_level(parent, product_bv, product_pv, ts,olderuser);

                        }
                    }
                    else if (user_status.Equals("2") && (id.Equals("1")))
                    {

                        Current_user = Session["user"].ToString();
                        DataAccess.users crnt = new DataAccess.users();
                        String CrntID = crnt.GetUserId(Current_user);
                        if ((id == CrntID))
                        {

                            TotalCommitions = direect_commision + T_commitioninDB;//total commition to child user
                            DataAccess.D_child ch = new DataAccess.D_child();
                            Boolean upchild = ch.Updatechild(id, TotalCommitions, product_bv, product_pv, direect_commision);// Add Update bonus 
                        }
                    }

            }

            catch (Exception err)
            {

                HttpContext.Current.Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
            }




        }
    }
}


